package com.iwdnb.gkgz.job;

import java.awt.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;

import cn.hutool.core.date.DateUtil;
import com.iwdnb.gkgz.application.service.StockEmotionService;
import com.iwdnb.gkgz.common.utils.DateUtils;
import com.iwdnb.job.annotation.Job;
import com.iwdnb.job.handler.BaseJobHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import static com.iwdnb.gkgz.common.utils.DateUtils.BEIGN_TIME;
import static com.iwdnb.gkgz.common.utils.WorkTimeUtils.isWorkingTime;

/**
 * 股票实时计算当天情绪信息
 */
@Slf4j
@Job(cron = "10 * 9,10,11,13,14,15 * * ?")
public class StockEmotionRealtimeJob extends BaseJobHandler {

    @Autowired
    private StockEmotionService stockEmotionService;

    @Override
    public boolean doProcess(String parameter) {
        log.info("StockEmotionRealtimeJob start");
        if (!isWorkingTime()) {
            return true;
        }
        stockEmotionService.refreshRealtimeStockEmotion();
        Date now = new Date();
        //每隔5分钟,打开一下情绪图浏览器,页面js会自动请求发送情绪图到钉钉，然后自动关闭
        int minute = DateUtil.minute(now);
        if (minute % 15 == 0) {
            String url = "http://root.com:8001/api/stock/chart/stockEmotionSaveImage?date=" + DateUtil.formatDate(now);
            try {
                Desktop.getDesktop().browse(new URI(url));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        log.info("StockEmotionRealtimeJob finished");
        return true;
    }
}
